package no.saiboten.tourbuilder.user.impl;

import java.util.Map;

import javax.annotation.Resource;

import no.saiboten.tourbuilder.user.User;
import no.saiboten.tourbuilder.user.UserDAO;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

@Service
public class UserDAOMock implements UserDAO {

	@Resource
	Map<String, User> userMap;

	final Logger LOGGER = Logger.getLogger(getClass());

	@Override
	public boolean insertUser(User user) {
		userMap.put(user.getUserName(), user);
		LOGGER.debug("Added user " + user);
		return true;
	}

	@Override
	public User getUser(String userid) {
		LOGGER.debug("Getting user " + userid);
		return userMap.get(userid);
	}

	@Override
	public boolean updateUser(User user) {
		if (userMap.containsKey(user.getUserName())) {
			LOGGER.debug("Updating user: " + user.getUserName());
			userMap.put(user.getUserName(), user);
			return true;
		}

		LOGGER.debug("Could not find user" + user.getUserName());
		return false;
	}

	@Override
	public boolean deleteUser(String user) {
		if (userMap.containsKey(user)) {
			userMap.remove(user);
			LOGGER.debug("Removed user " + user);
			return true;
		}
		LOGGER.debug("Could not find user: " + user);
		return false;
	}

	@Override
	public Map<String, User> getAllUsers() {
		return userMap;
	}

}
